﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.SecurityEvaluate;
using Yipusoft.Vehicle.BLL;
using Yipusoft.Vehicle.Model;
using Yipusoft.BasicBase.BLL;
using Yipusoft.BasicBase.Model;
using Yipusoft.Account.BLL;
using Yipusoft.Account.Model;

public partial class DutyLog_ParkLogView1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDatalist();

        }
    }



    void BindDatalist()
    {
        string DepartmentId = Request["departmentId"];
        DateTime date = Convert.ToDateTime(Request["Date"]);
        ParkLogDataContext pldc = new ParkLogDataContext();
        //获取车场日
        var cc = pldc.ParkLogs.Where(o => o.Date.Value.Year == date.Year && o.Date.Value.Month == date.Month && o.Date.Value.Day == date.Day && o.DepartmentID == DepartmentId);
        ParkLogs log = cc.FirstOrDefault();
        if (log != null)
        {
            //日期
            lbldate.Text = date.ToShortDateString();
            //单位名称
            lblDepartment.Text = log.DepartmentName;
            //签字
            lblSign.Text = log.Captain;
            lblRemark.Text = log.OtherDevices == "" || log.OtherDevices == null ? "无" : log.OtherDevices;
            List<CarOrder> orders = pldc.CarOrder.Where(o => o.LogID == log.ID).ToList();
            dl.DataSource = orders;
            dl.DataBind();
            //是否签字
            string Departmentid = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;

            DateTime CurrentStartDate = new DateTime();
            if (Convert.ToInt32(DateTime.Now.DayOfWeek) == 0)
            {
                CurrentStartDate = Convert.ToDateTime(DateTime.Now.AddDays(-7).ToShortDateString());
            }
            else
            {
                CurrentStartDate = Convert.ToDateTime(DateTime.Now.AddDays(-1 * Convert.ToInt32(DateTime.Now.DayOfWeek)).ToShortDateString());
            }

            if (DateTime.Now.DayOfWeek == DayOfWeek.Friday || DateTime.Now.DayOfWeek == DayOfWeek.Saturday || DateTime.Now.DayOfWeek == DayOfWeek.Sunday || DateTime.Now.DayOfWeek == DayOfWeek.Monday)
            {
                if (log.Date.Value == CurrentStartDate.AddDays(5) || log.Date.Value == CurrentStartDate.AddDays(6) || log.Date.Value == CurrentStartDate.AddDays(7) || isDataOk(log, CurrentStartDate))
                {
                    if ((log.Captain == null || log.Captain.Trim() == "") && isOk(Departmentid, log))
                    {
                        Button1.Visible = true;
                    }
                    else
                    {
                        Button1.Visible = false;
                    }
                }
                else
                {
                    Button1.Visible = false;
                }
            }
            else
            {
                Button1.Visible = false;
            }
        }
        else
        {

        }
    }

    bool isDataOk(ParkLogs log, DateTime CurrentStartDate)
    {
        if ((log.Date.Value == CurrentStartDate.AddDays(-1) || log.Date.Value == CurrentStartDate.AddDays(-2) || log.Date.Value == CurrentStartDate) && log.DepartmentID.Substring(0, 4) == "1308")
        {
            return true;
        }
        else
        {
            return false;
        }
    }


    bool isOk(string departmentid, ParkLogs park)
    {
        if (park.DepartmentID.Trim() == departmentid.Trim())
        {
            return true;
        }
        else
        {
            if (departmentid.Substring(4, 1) == "2")
            {
                string ShortName = Yipusoft.Account.BLL.Department.GetDepartment(departmentid).ShortName.Substring(0, 2);
                List<DepartmentInfo> departments = Yipusoft.Account.BLL.Department.GetList(" departmentid like '" + departmentid.Substring(0, 4) + "3__' and ShortName like '" + ShortName + "%'  ").ToList();
                if (departments.Count > 0)
                {
                    bool ok = false;
                    for (int i = 0; i < departments.Count; i++)
                    {
                        if (departments[i].DepartmentID.Trim() == park.DepartmentID.Trim())
                        {
                            ok = true;
                            break;
                        }
                    }
                    return ok;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
    }

    protected void dl_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {

            CarOrder order = e.Item.DataItem as CarOrder;
            //车辆
            Label lblVehcle = e.Item.FindControl("lblVehcle") as Label;
            VehicleInfo vehicle = Vehicle.GetDetail(new Guid(order.CarID));
            if (vehicle == null)
            {
                lblVehcle.Text = "此车辆已从数据库中删除！";
            }
            else
            {
                lblVehcle.Text = vehicle.VehicleName + "(" + vehicle.NumberPlate + ")";
            }

            //情况
            Label lblCircs = e.Item.FindControl("lblCircs") as Label;
            if (order.InspectState == 1)
            {

                ParkLogDataContext pldc = new ParkLogDataContext();
                List<CarInspect> inspects = pldc.CarInspect.Where(o => o.CID.Value == order.ID && o.Capability.Value == 2).OrderBy(o => o.Amount).ToList();
                for (int i = 0; i < inspects.Count; i++)
                {
                    string inspectName = "";
                    if (inspects[i].Amount.Value > 0)
                    {
                        inspectName = Device.GetDevice(inspects[i].EID.Value).DeviceName + "(数量：" + inspects[i].Amount + ")";
                    }
                    else
                    {
                        inspectName = pldc.Equipment.Where(o => o.ID == inspects[i].EID.Value).FirstOrDefault().ItemName;
                    }
                    lblCircs.Text += inspectName + "&nbsp;&nbsp;&nbsp;&nbsp;" + inspects[i].Remark + "<br/>";
                }
            }
            else
            {
                lblCircs.Text = "完好";
            }

        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string DepartmentID = Request["departmentId"];
        int cuDay = Convert.ToInt32(DateTime.Parse(Request["Date"]).DayOfWeek);
        int temp = 0;
        if (cuDay == 0)
        {
             temp = -6;
        }
        else
        {
             temp = (-1) * cuDay + 1;

        }

        DateTime StartTime = DateTime.Parse(DateTime.Parse(Request["Date"]).AddDays(temp).ToString("yyyy-MM-dd"));
        DateTime EndTime = DateTime.Parse(StartTime.AddDays(7).ToString("yyyy-MM-dd"));
        CarOrder order = Park.GetParkLog2(StartTime, EndTime, DepartmentID);
        if (order == null)
            return;

        ParkLogs park = Park.GetParkLog(StartTime, EndTime, DepartmentID);
        if (park != null)
        {
            Yipusoft.SecurityEvaluate.ParkLogDataContext db = new ParkLogDataContext();
            park = db.ParkLogs.Where(o => o.ID == park.ID).FirstOrDefault();
            park.Captain = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).UserName;
            db.SubmitChanges();
        }
        else
        {
            park = new ParkLogs();
            park.ID = Guid.NewGuid();
            park.SetDate = DateTime.Now;
            park.SetUsers = order.UserID;
            park.DepartmentName = order.DepartmentName;
            park.DepartmentID = DepartmentID;
            park.Date = order.Date;
            park.Captain = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).UserName;


            Park.Insert(park);
            //Yipusoft.SecurityEvaluate.ParkLogDataContext db = new ParkLogDataContext();



        }
        BindDatalist();
    }
}
